package cn.wolfcode.wolf2w.strategy.service.impl;

import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.core.utils.StringUtils;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import cn.wolfcode.wolf2w.strategy.entity.StrategyComment;
import cn.wolfcode.wolf2w.strategy.mapper.StrategyCommentMapper;
import cn.wolfcode.wolf2w.strategy.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.strategy.service.IStrategyCommentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 攻略评论Service业务层处理
 * 
 * @author libai
 * @date 2024-07-24
 */
@Service
@Transactional
public class StrategyCommentServiceImpl extends ServiceImpl<StrategyCommentMapper, StrategyComment> implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper commentMapper;

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Autowired
    private RedisService redisService;

    @Override
    public IPage<StrategyComment> queryPage(StrategyCommentQuery qo) {
        IPage<StrategyComment> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        IPage<StrategyComment> pageResult= lambdaQuery()
                .eq(StrategyComment::getStrategyId,qo.getStrategyId())
                .page(page);
        for (StrategyComment comment : pageResult.getRecords()) {
            R<UserInfo> r = remoteUserInfoService.getOne(comment.getUserId(), SecurityConstants.INNER);
            if (r.getCode() == 200){
                comment.setUser(r.getData());
            }

            if (!StringUtils.isBlank(comment.getThumbupids())){
                String[] arr = comment.getThumbupids().split(",");
                List<Long> thumbupidsList = comment.getThumbupidsList();
                for (String s : arr) {

                    thumbupidsList.add(Long.valueOf(s));
                }
            }
        }
        return pageResult;
    }

    @Override
    public void addComment(StrategyComment comment, Long uid) {
        String content = StringUtils.trimToNull(comment.getContent());
        Assert.notNull(content,"评论内容不能为空!!");
        comment.setUserId(uid);
        comment.setCreateTime( new Date());
        comment.setThumbnum(0l);
        comment.setState(1l);
        commentMapper.insert(comment);
    }
}
